package com.smart.controller;


import com.smart.pojo.Permission;
import com.smart.pojo.ResultBean;
import com.smart.service.IPermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 获取全部权限信息
     *
     * @return
     */
    @GetMapping("queryAll")
    public ResultBean queryAll() {
        System.out.println("获取全部权限信息");
        return ResultBean.success(permissionService.lambdaQuery().orderByAsc(Permission::getSortNo).list());
    }

    /**
     * 根据权限，名称查询权限列表
     *
     * @param keyword
     * @return
     */
    @GetMapping("queryByPermissionName")
    public ResultBean queryByPermissionName(String keyword) {
        System.out.println("查询权限的名称：" + keyword);
        List<Permission> permissionList = permissionService.lambdaQuery()
                .like(!ObjectUtils.isEmpty(keyword), Permission::getName, keyword)
                .orderByAsc(Permission::getSortNo)
                .list();
        return ResultBean.success(permissionList);
    }

    /**
     * 新增或修改权限信息
     *
     * @param permission
     * @return
     */
    @PostMapping("saveOrUpdate")
    @RequiresPermissions({"permission:save"})
    public ResultBean saveOrUpdate(@RequestBody Permission permission) {
        System.out.println("新增或修改的权限：" + permission);
        boolean rs = permissionService.saveOrUpdatePermission(permission);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据id删除权限信息
     */
    @DeleteMapping("deleteById/{id}")
    @RequiresPermissions({"permission:del"})
    public ResultBean deleteById(@PathVariable("id") Long id) {
        System.out.println("要删除的权限的Id：" + id);
        boolean rs = permissionService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据角色Id获取权限列表
     */
    @GetMapping("queryByRoleId/{roleId}")
    public ResultBean queryByRoleId(@PathVariable("roleId") Long roleId) {
        System.out.println("要获取权限列表的角色Id：" + roleId);
        List<Permission> permissionList = permissionService.queryByRoleId(roleId);
        return ResultBean.success(permissionList);
    }

    /**
     * 根据用户ID获取权限
     */
    @GetMapping("queryByUserId/{userId}")
    public ResultBean queryByUserId(@PathVariable("userId") Long userId) {
        System.out.println("获取权的用户ID：" + userId);
        List<Permission> permissionList = permissionService.queryByUserId(userId);
        permissionList.sort(Comparator.comparingInt(Permission::getSortNo));
        return ResultBean.success(permissionList);
    }
}
